package com.yenlien.traceyun.milk.controller;

import com.yenlien.traceyun.business.interceptor.UserAuthUtil;
import com.yenlien.traceyun.business.utils.EasyPoiUtils;
import com.yenlien.traceyun.milk.entity.MilkBucket;
import com.yenlien.traceyun.milk.service.MilkBucketService;
import com.yenlien.traceyun.webase.controller.BaseController;
import io.swagger.annotations.Api;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;

/**
 * @Date 2022/7/11
 * @Author jiaLong
 * @Descript RFID标签
 */
@Api(tags = "RFID标签")
@RestController
@RequestMapping("/milkBucket")
public class MilkBucketController extends BaseController {


    @Autowired
    private MilkBucketService milkBucketService;

    @PostMapping(value = "/save")
    public Object save(@RequestBody MilkBucket milkBucket) {
        return success(milkBucketService.save(milkBucket));
    }

    @PostMapping(value = "/findByList")
    public Object findByList(@RequestBody MilkBucket milkBucket) {
        if(StringUtils.isBlank(milkBucket.getCompanyId())){
            milkBucket.setCompanyId(UserAuthUtil.getLoginUser().getCompanyId());
        }
        return success(milkBucketService.findByList(milkBucket));
    }

    @GetMapping(value = "/count")
    public Object count(String companyId) {
        if(StringUtils.isBlank(companyId)){
            companyId = UserAuthUtil.getLoginUser().getCompanyId();
        }
        return success(milkBucketService.count(companyId));
    }

    @PostMapping(value = "/import")
    public Object importMilk(HttpServletRequest request) {
        milkBucketService.importMilk(((MultipartHttpServletRequest) request).getFile("file"), UserAuthUtil.getLoginUser().getCompanyId(), UserAuthUtil.getLoginUser().getId());
        return success("success");
    }
    // 下载导出模板
    @GetMapping(value = "/exportTemplate")
    public void exportTemplate(HttpServletResponse response) {
        try {
            EasyPoiUtils.exportExcel(new ArrayList<MilkBucket>(), "RFID管理导入模板", "RFID管理导入模板", MilkBucket.class, "RFID管理导入模板", response);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
